package com.mybatisplus.demo.android.controller;

import com.alibaba.druid.util.StringUtils;
import com.mybatisplus.demo.android.enmu.ApiResponse;
import com.mybatisplus.demo.android.enmu.ResultCode;
import com.mybatisplus.demo.android.model.FileInfo;
import com.mybatisplus.demo.android.service.FileService;
import com.mybatisplus.demo.android.util.ApiResponseUtil;
import com.mybatisplus.demo.android.util.FileUploadUtil;
import com.mybatisplus.demo.android.util.LoggerUtil;
import com.mybatisplus.demo.android.viewobject.FileVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文件上传、下载
 * Spring Boot默认文件上传大小为2M，多文档上传中总是出现文件大小超出限度
 * 在启动类DemoApplication.class文件中配置Bean来设置文件大小
 */
@Controller
@Api(value = "文件上传接口", description = "批量上传", tags = "文件管理接口")
public class FileUploadController {

    @Autowired
    private FileService fileService;


    /**
     * 单个文件上传
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/upload/file")
    @ApiOperation(value = "文件上传接口", httpMethod = "POST", response = ApiResponse.class, consumes = "multipart/form-data")
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"success\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 404, message = "{\"code\":404,\"message\":\"未知错误\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 216, message = "{\"code\":216,\"message\":\"文件不能为空\",\"data\":null}")
    })
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "HttpServletRequest", value = "上传的文件为List<MultipartFile>", required = true, dataType = "File", paramType = "form"),
    })
    public ApiResponse fileUpload(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if (StringUtils.isEmpty(userId)) {
            return new ApiResponse(ResultCode.ERROR);
        }
        //获paramType取上传的文件数组
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        if (files.size() == 0) {
            ApiResponse apiResponse = new ApiResponse();
            apiResponse.setResultCode(ResultCode.ERROR);
            apiResponse.setMessage("文件不能为空");
            return ApiResponseUtil.getResult(apiResponse);
        }
        LoggerUtil.info(" userId ---- " + userId);
        //返回文件地址集合 保存到本地 将url存入数据库
        List<FileInfo> filepath = FileUploadUtil.saveFiles(files, userId);
        if (filepath.size() == 0) {
            ApiResponse apiResponse = new ApiResponse();
            apiResponse.setResultCode(ResultCode.ERROR);
            apiResponse.setMessage("文件存储异常");
            return ApiResponseUtil.getResult(apiResponse);
        }
        ApiResponse apiResponse = fileService.addFiles(filepath);
        return ApiResponseUtil.getResult(apiResponse);
    }


}
